package com.sdsy.util;

import cn.hutool.core.exceptions.UtilException;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

public class MD5Utils {
	private static final String SALT = "wallet2018&@0804";

	private static final String ALGORITH_NAME = "md5";

	private static final int HASH_ITERATIONS = 2159;
	
	public static final String ACCOUNT_SALT = "ACCOUT&AMOUNT"; // 前缀
	public static final int ACCOUNT_HASH_ITERATIONS = 666666; // 后缀

	/**
	 * 密码加密
	 * @param pswd
	 * @return
	 */
	public static String encrypt(String pswd) {
		String newPassword = new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(SALT), HASH_ITERATIONS).toHex();
		return newPassword;
	}

	public static String encrypt(String username, String pswd) {
		String newPassword = new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(username + SALT),
				HASH_ITERATIONS).toHex();
		return newPassword;
	}
	
	
	/**
	 * 金额加密
	 * @param freeNum
	 * @param frozenNum
	 * @param encryptBalance
	 * @throws UtilException
	 */
	public static boolean checkBalance(String freeNum, String frozenNum, String encryptBalance) {
		StringBuffer sb = new StringBuffer().append(ACCOUNT_SALT).append(freeNum).append(frozenNum).append(ACCOUNT_HASH_ITERATIONS);
		if(!DigestUtils.md5Hex(sb.toString()).equals(encryptBalance)) {
			return false;
		}
		return true;
	}
    
	
	public static String encryptBalance(String freeNum, String frozenNum) {
		StringBuffer sb = new StringBuffer().append(ACCOUNT_SALT).append(freeNum).append(frozenNum).append(ACCOUNT_HASH_ITERATIONS);
		return DigestUtils.md5Hex(sb.toString());
	}
	
	public static void main(String[] args) {
		System.out.println(MD5Utils.encrypt("18318721581", "123456"));
		System.out.println(MD5Utils.encrypt("123456"));
		
		System.out.println(encryptBalance("527084.416261", "0.000000"));
	}
	
	

}
